package com.mtiiot.fsmanage.controller;

import com.mtiiot.fsmanage.bean.parameter.User;
import com.mtiiot.fsmanage.server.ParUserServer;
import com.mtiiot.fsmanage.utils.MD5Common;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@SuppressWarnings("all")
@Controller
public class ParUserController{
	private static Logger logger = LogManager.getLogger(ParUserController.class);
	@Autowired
    ParUserServer parUserServer;
	

	/** 
     * 登录验证 
     * @param 
     * @return 
     */  
	@RequestMapping("main.action")
	public String login(String userName,String userPwd,HttpServletRequest r)throws Exception{
		String returnStr=null;
		if(r.getSession().getAttribute("user")!=null){
			returnStr = "main";
		}
		// 加密
		if (StringUtils.isNotBlank(userPwd)){
			userPwd = MD5Common.encryptMD5(userPwd);
		}
		User u=parUserServer.queryUser(userName, userPwd);
		if(u!=null){
			r.getSession().setAttribute("user", u);
			returnStr = "main";
		}else{
			r.setAttribute("validateFailure", "用户名或者密码错误!");
			returnStr = "login";
		}
		if("main".equals(returnStr)){
			User user= (User) r.getSession().getAttribute("user");
			r.setAttribute("user", u);
		}
		return returnStr;
	}
}